/**
 * sleep休眠函数 
 */
function sleep(ms) {
	var endTime  = new Date().getTime() + parseInt(ms); 

	while(new Date().getTime() < endTime) ;
}


/*
// 下面的循环为死循环， then中的代码是异步的，
// 是在 while 代码执行完后，后面才执行的, while中并没有改变i
var i = 1
var p = Promise.resolve(i);
while(i <= 3) {
	p = p.then(value => {
		console.log(`i = ${i++}, value = ${value}`)

		if(i == 3) {
			console.log('退出了-----------------------\n')
		}
	})
}
*/

/*

while(chain.length) {
	promise = promise.then(chain.shift(), chain.shift())
}
return promise
*/

var i = 1
var p = Promise.resolve(i);
while(i <= 3) {
	// 弄一个自运行函数
	(function(i) {
		p = p.then(value => {
			console.log(`i = ${i}, value = ${value}`)

			if(i == 3) {
				console.log('退出了-----------------------\n')
			}
		})
	})(i++)
}


/*
p = p.then(value => {
	console.log(`i = ${i++}, value = ${value}`)
})


p = p.then(value => {
	console.log(`i = ${i++}, value = ${value}`)
})


p = p.then(value => {
	console.log(`i = ${i++}, value = ${value}`)
})

console.log('hahah')


*/
